package com.wwwarehouse.taskcenter.fragment.job_point.step2.parent;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.job_point.WarehousePointResponseBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;
import com.wwwarehouse.taskcenter.constant.job_point.TaskCenterCommon;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.wwwarehouse.taskcenter.fragment.job_point.AddJobPointCardFragment.KEY_BUSINESS_ID;
import static com.wwwarehouse.taskcenter.fragment.job_point.AddJobPointCardFragment.KEY_WAREHOUSE_UKID;

/**
 * Created by Jackie on 2017/10/27
 * 父作业点位置维度  选择添加方式
 */

public class ChooseAddWayParentPositionFragment extends BaseFragment implements View.OnClickListener {
    private View mRootView;

    private String mBusinessId;
    private String mWarehouseUkid;

    private RelativeLayout mWayAnyLayout;
    private RelativeLayout mWayWarehouseLayout;

    private List<WarehousePointResponseBean> mWarehousePointDetailsList;

    private static final int REQUEST_WAREHOUSE_POINT = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_choose_add_way_parent_position, container, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initData();
        initEvent();
    }

    private void initView() {
        mWayAnyLayout = findView(mRootView, R.id.rl_way_any);
        mWayWarehouseLayout = findView(mRootView, R.id.rl_way_warehouse);

        mWarehousePointDetailsList = new ArrayList<>();
    }

    private void initData() {
        Bundle bundle = getArguments();

        if (bundle != null) {
            mBusinessId = bundle.getString(KEY_BUSINESS_ID);
            mWarehouseUkid = bundle.getString(KEY_WAREHOUSE_UKID);
        }
    }

    private void initEvent() {
        mWayAnyLayout.setOnClickListener(this);
        mWayWarehouseLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.rl_way_any) {

            //获取所有仓点信息
            getWarehousePointInfo();
        } else if (id == R.id.rl_way_warehouse) {
            //按仓库点添加
            Bundle bundle = new Bundle();
            bundle.putString(KEY_BUSINESS_ID, mBusinessId);
            bundle.putString(KEY_WAREHOUSE_UKID, mWarehouseUkid);

            ChooseAddWayParentPositionByWarehousePointFragment chooseAddWayParentPositionByWarehousePointFragment = new ChooseAddWayParentPositionByWarehousePointFragment();
            chooseAddWayParentPositionByWarehousePointFragment.setArguments(bundle);
            pushFragment(chooseAddWayParentPositionByWarehousePointFragment);
        }
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ChooseAddWayParentPositionFragment) {
            mActivity.setTitle(R.string.choose_add_type);
        }
    }

    @Override
    public void requestDatas() {

    }

    private NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case REQUEST_WAREHOUSE_POINT:
                    if ("0".equals(commonClass.getCode())) {
                        mWarehousePointDetailsList = JSON.parseArray(commonClass.getData().toString(), WarehousePointResponseBean.class);

                        popFragment();

                        buildAnyMap(mWarehousePointDetailsList);

                        Intent intent = new Intent("ChooseAddWayParentPositionByAny");
                        mActivity.sendBroadcast(intent);
                    } else {
                        toast(commonClass.getMsg());
                    }
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onFailed(String result, int what) {
            toast(result);
        }

        @Override
        public void onFinish(int what) {
            dismissProgressDialog();
        }
    };

    private void getWarehousePointInfo() {
        showProgressDialog();

        Map map = new HashMap();

        map.put("buId", mBusinessId);

        NoHttpUtils.httpPost(TaskCenterConstant.URL_GET_WAREHOUSE_POINT_INFO, map, mOnResponseListener, REQUEST_WAREHOUSE_POINT);
    }

    private void buildAnyMap(List<WarehousePointResponseBean> warehousePointDetailsList) {
        //根据当前显示的仓库stockId 找到相应的pointUkid 放到startMap中
        Map startMap = new HashMap();
        List<Map<String, String>> startList = new ArrayList<>();

        for (WarehousePointResponseBean warehousePointDetailsBean : warehousePointDetailsList) {
            if (mWarehouseUkid.equals(warehousePointDetailsBean.getStockUkid())) {
                Map<String, String> listMap = new HashMap<>();

                listMap.put("key", warehousePointDetailsBean.getPointUkid());
                listMap.put("value", warehousePointDetailsBean.getStockName());

                startList.add(listMap);
            }
        }

        startMap.put("list", startList);
        startMap.put("type", "Position_POINT");

        TaskCenterCommon.getInstance().setStartMap(startMap);

        Map endMap = new HashMap();

        List<Map<String, String>> endList = new ArrayList<>();

        Map<String, String> listMap = new HashMap<>();

        listMap.put("key", "-10002");
        listMap.put("value", "任意");

        endList.add(listMap);

        endMap.put("list", endList);
        endMap.put("type", "CONSTANT");

        TaskCenterCommon.getInstance().setEndMap(endMap);
    }
}
